package thomas;

import org.apache.pivot.beans.BXMLSerializer;
import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Application.UnprocessedKeyHandler;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Keyboard.KeyLocation;
import org.apache.pivot.wtk.Theme;

import com.openroom.ui.FocusableImageView;
import com.openroom.ui.FocusableImageViewSkin;

public class FocusableImageViewExample implements Application, UnprocessedKeyHandler {

	private static final String BXML_FILE = "focusable_image_view_example.bxml";
	private static final Class<?> EXPECTED_ROOT_CLASS = FocusableImageViewWindow.class;

	// Add our custom Component & Skin to the theme.
	public FocusableImageViewExample() {
		Theme.getTheme().set(FocusableImageView.class, FocusableImageViewSkin.class);
	}

	@Override
	public void startup(Display display, Map<String, String> properties) throws Exception {
		// Load the BXML file
		final BXMLSerializer serializer = new BXMLSerializer();
		final Object root = serializer.readObject(FocusableImageViewExample.class, BXML_FILE);
		if (root == null || !EXPECTED_ROOT_CLASS.isAssignableFrom(root.getClass())) {
			String message = String.format("Incorrect GUI root object type. '%s' was expected, but '%s' was found.", EXPECTED_ROOT_CLASS.getName(), root == null ? "[null]" : root.getClass().getName());
			throw new IllegalStateException(message);
		}
		// Open the Window
		FocusableImageViewWindow guiWindow = (FocusableImageViewWindow) root;
		guiWindow.open(display);
	}


	@Override
	public boolean shutdown(boolean optional) throws Exception {
		return false;
	}
	@Override
	public void suspend() throws Exception {
	}
	@Override
	public void resume() throws Exception {
	}


	// Application.UnprocessedKeyHandler
	// These methods will be called if no Component has focus when keyboard events occur.
	@Override
	public void keyPressed(int keyCode, KeyLocation keyLocation) {
		//System.out.println(String.format(FocusableImageViewWindow.KEYPRESSED_MESSAGE, "keyPressed", keyCode, "[none]"));
	}
	@Override
	public void keyTyped(char character) {
	}
	@Override
	public void keyReleased(int keyCode, KeyLocation keyLocation) {
	}


	public static void main(String[] args) {
		// Enable the 'debug focus' decorator by setting a System property
		// which will be processed when the ApplicationContext is created
		//System.setProperty("org.apache.pivot.wtk.debugfocus", "true");
		DesktopApplicationContext.main(FocusableImageViewExample.class, args);
	}
}